package com.anlogic.sdk.startup;

import org.eclipse.core.runtime.IProduct;
import org.eclipse.core.runtime.Platform;
import org.eclipse.e4.ui.css.swt.CSSSWTConstants;
import org.eclipse.jface.resource.StringConverter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.internal.splash.EclipseSplashHandler;

import com.anlogic.sdk.utils.EnvUtils;

@SuppressWarnings("restriction")
public class IDESplashHandler extends EclipseSplashHandler {
	private static final String SPLASH_CUSTOMIZE_MESSAGE_ID = "org-eclipse-ui-splash-customizeMessage";

	String SHOW_CUSTOMIZE_MESSAGE = "showCustomizeMessage";
	String CUSTOMIZE_MESSAGE_RECT = "customizeMessageRect";
	String CUSTOMIZE_MESSAGE = "customizeMessage";

	@Override
	public void init(Shell splash) {
		super.init(splash);

		IProduct product = Platform.getProduct();
		if (product == null) {
			return;
		}

		String enable = product.getProperty(SHOW_CUSTOMIZE_MESSAGE);

		if (enable.equals("true")) {
			String ideVersion = EnvUtils.getIDEVersion();
			String messageRect = product.getProperty(CUSTOMIZE_MESSAGE_RECT);
			String message = product.getProperty(CUSTOMIZE_MESSAGE);

			Rectangle messageRectangle = StringConverter.asRectangle(messageRect, new Rectangle(30, 350, 300, 20));
			Rectangle versionRectangle = new Rectangle(messageRectangle.x, messageRectangle.y - 20,
					messageRectangle.width, 20);

			Label versionLabel = new Label(getContent(), SWT.LEFT);
			Label messageLabel = new Label(getContent(), SWT.LEFT);

			Font versionFont = new Font(getContent().getDisplay(), new FontData("Arial", 10, SWT.BOLD));
			Font messageFont = new Font(getContent().getDisplay(), new FontData("Arial", 10, SWT.NORMAL));

			versionLabel.setFont(versionFont);
			versionLabel.setForeground(new Color(null, 0, 0, 0));
			versionLabel.setBounds(versionRectangle);
			versionLabel.setText(ideVersion);
			versionLabel.setData(CSSSWTConstants.CSS_ID_KEY, SPLASH_CUSTOMIZE_MESSAGE_ID);

			messageLabel.setFont(messageFont);
			messageLabel.setForeground(new Color(null, 0, 0, 0));
			messageLabel.setBounds(messageRectangle);
			messageLabel.setText(message);
			messageLabel.setData(CSSSWTConstants.CSS_ID_KEY, SPLASH_CUSTOMIZE_MESSAGE_ID);
		} else {
			getContent();
		}
	}
}
